let user
const app = getApp()
const api = wx.getStorageSync('api')
Page({
  data: {
    price: parseInt('64950'),
    address: null
  },

  onLoad: function (options) {
    user = wx.getStorageSync('user')
    this.getCartList()
  },

  onShow() {
    this.getAddress()
  },

  /**
   * 获取收货地址
   */
  getAddress() {
    const url = api.address_get + `?userId=${user.id}`
    app.$http.get(url).then(res => {
      console.log(res);
      this.setData({
        address: res.data
      })
    })
  },

  /**
   * 获取购物车勾选商品
   */
  getCartList() {
    const url = api.cart + `?userId=${user.id}&choose=true`
    app.$http.get(url).then(res => {
      this.setData({
        ...res.data
      })
      let totalPrice = this.data.totalPrice + ''
      totalPrice = parseInt(totalPrice.replace('.', ''))
      this.setData({
        totalPrice: totalPrice * 100
      })
    })
  },

  /**
   * 跳转到收货地址页
   */
  toAddress() {
    wx.navigateTo({
      url: '/pages/address/address'
    })
  },

  /**
   * 提交生成订单
   */
  onSubmit() {
    const url = api.order_create + `?userId=${user.id}`
    app.$http.post(url).then(res => {
      console.log(res)
      if (res.code == 401) {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
        return
      }
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/orderList/orderList'
        })
      }, 1500)
    })
  }
})